package com.kaiyidiannao.activity.user_detail;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.kaiyidiannao.adapter.JoinConstactAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.JoinConstact;
import com.kaiyidiannao.model.req.model.GetJoinConstact;
import com.kaiyidiannao.view.ActionBarView;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by mark on 2014/8/5.
 */
public class JoinConstactActivity extends BaseActionBarActivity implements Observer {

    private DragListView baseList;
    private ArrayList<JoinConstact> joins = DataCenter.joinConstacts;
    private int currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private JoinConstactAdapter joinConstactAdapter;
    private View no_content_view;
    String userid;
    String itemid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.join_constact);
        DataCenter.getInstance().addObserver(this);
        Intent intent = getIntent();
        userid = intent.getStringExtra("userid");
        itemid = intent.getStringExtra("itemid");
        findId();
        new GetJoinConstact(userid, itemid, "53", currentPage).execute();
    }

    public void findId() {
        no_content_view = findViewById(R.id.no_content_view);
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("加入联系人");
        baseList = (DragListView) findViewById(R.id.baseList);

        baseList.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                if (DataCenter.userLogin.size() != 0) {
                    currentPage = 1;
                    new GetJoinConstact(userid, itemid, "53", currentPage).execute();
                } else {
                    showToast(getText(R.string.no_network).toString());
                }
            }

            @Override
            public void onLoadMore() {
                if (DataCenter.userLogin.size() != 0) {
                    new GetJoinConstact(userid, itemid, "53", ++currentPage).execute();
                } else {
                    showToast(getText(R.string.no_network).toString());
                }
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        if (!HttpUtils.isConnected()) {
            baseList.onRefreshComplete();
            baseList.onLoadMoreComplete(false);
        }
        if (flag == DataCenter.flag.GET_JOIN_CONSTACT_SUCCESS || flag == DataCenter.flag.GET_JOIN_CONSTACT_FAILED) {
            if (flag == DataCenter.flag.GET_JOIN_CONSTACT_SUCCESS) {
                if (joins.size() == 0 && currentPage <= 1) {
                    no_content_view.setVisibility(View.VISIBLE);
                    baseList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(R.string.listview_msg_empty_pub);
                } else {
                    baseList.onLoadMoreComplete(false);
                    baseList.onRefreshComplete();
                    no_content_view.setVisibility(View.GONE);
                    baseList.setVisibility(View.VISIBLE);
                    joinConstactAdapter = new JoinConstactAdapter(this, joins);
                    baseList.setAdapter(joinConstactAdapter);
                    joinConstactAdapter.notifyDataSetChanged();
                }

            }
        }

    }
}
